package de.ifgi.tank;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/**
 * This class represents the tank as an Android view. The class draws lines,
 * rectangles and images that altogether look like a tank.
 * 
 * @author matthias.robbers@wwu.de
 * 
 */
public class TankView extends View {
	Paint paint;
	Path path;
	public boolean l1, l2, l3, l4, l5, l6 = false;
	boolean step = true;
	int top = 55;
	int left = 10;
	int trackWidth = 80;
	int bodyExcess = 15;
	int bodyLeft = left + trackWidth - 10;
	int bodyWidth = 152;
	int bodyRight = bodyLeft + bodyWidth + 10;
	int bodyHeight = 370;
	int bodyTop = top;
	int bodyBottom = bodyTop + bodyHeight;
	int trackHeight = bodyHeight - bodyExcess * 2;
	int trackTop = top + bodyExcess;
	int trackBottom = trackTop + trackHeight;
	int lightSpace = 10;
	int lightWidth = Math.round((bodyWidth + trackWidth * 2) / 3) - lightSpace
			+ 3;
	int lightHeight = 30;

	public TankView(Context context) {
		super(context);
		init();
	}

	public TankView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public TankView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		paint = new Paint();
	}

	public void update() {
		bodyWidth = 600;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawTracks(canvas);
		drawBody(canvas);
		drawLights(canvas);
	}

	private void drawTracks(Canvas canvas) {
		int color = Color.parseColor("#444444");
		paint.setStrokeWidth(7);
		paint.setColor(color);
		paint.setStyle(Style.STROKE);
		canvas.drawRect(left, trackTop - 5, left + trackWidth, trackBottom + 5,
				paint);

		canvas.drawRect(bodyLeft + bodyWidth, trackTop - 5, bodyLeft
				+ bodyWidth + trackWidth, trackBottom + 5, paint);

		paint.setStrokeWidth(10);

		int colorA = Color.parseColor("#444444");
		int colorB = Color.parseColor("#111111");
		// draw left track
		if (step) {
			paint.setColor(colorA);
		} else {
			paint.setColor(colorB);
		}
		for (int i = trackTop; i <= trackBottom; i += 20) {
			Path p = new Path();
			p.moveTo(left, i);
			p.lineTo(left + trackWidth, i);
			canvas.drawPath(p, paint);
		}

		if (step) {
			paint.setColor(colorB);
		} else {
			paint.setColor(colorA);
		}
		for (int i = trackTop + 10; i <= trackBottom; i += 20) {
			Path p = new Path();
			p.moveTo(left, i);
			p.lineTo(left + trackWidth, i);
			canvas.drawPath(p, paint);
		}

		// draw right track
		if (step) {
			paint.setColor(colorA);
		} else {
			paint.setColor(colorB);
		}
		for (int i = trackTop; i <= trackBottom; i += 20) {
			Path p = new Path();
			p.moveTo(bodyLeft + bodyWidth, i);
			p.lineTo(bodyLeft + bodyWidth + trackWidth, i);
			canvas.drawPath(p, paint);
		}

		if (step) {
			paint.setColor(colorB);
		} else {
			paint.setColor(colorA);
		}
		for (int i = trackTop + 10; i <= trackBottom; i += 20) {
			Path p = new Path();
			p.moveTo(bodyLeft + bodyWidth, i);
			p.lineTo(bodyLeft + bodyWidth + trackWidth, i);
			canvas.drawPath(p, paint);
		}
	}

	private void drawBody(Canvas canvas) {
		paint.setColor(Color.parseColor("#00979D"));
		paint.setStyle(Style.FILL);
		canvas.drawRect(bodyLeft, bodyTop, bodyRight, bodyBottom, paint);

		paint.setColor(Color.parseColor("#00878D"));
		paint.setStrokeWidth(5);
		paint.setStyle(Style.STROKE);
		canvas.drawRect(bodyLeft, bodyTop, bodyRight, bodyBottom, paint);

		// scale bitmap
		int h = 800; // height in pixels
		int w = 480; // width in pixels
		Resources res = getResources();
		Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.arduino);
		Bitmap scaled = Bitmap.createScaledBitmap(bitmap,
				bitmap.getWidth() / 2, bitmap.getHeight() / 2, true);
		// Bitmap scaled = Bitmap.createScaledBitmap(mBackground, h, w, true);
		canvas.drawBitmap(scaled, bodyLeft + bodyWidth / 2 - scaled.getWidth()
				/ 2 + 4, bodyTop + bodyHeight / 2 - scaled.getHeight() / 2,
				paint);
	}

	private void drawLights(Canvas canvas) {
		int colorA = Color.parseColor("#330000");
		int colorB = Color.parseColor("#FF0000");
		paint.setStyle(Style.FILL);
		// draw top lights
		int l = left - 4;
		int t = top - 45;

		paint.setColor(colorA);
		if (l1)
			paint.setColor(colorB);
		canvas.drawRect(l, t, l += lightWidth, t + lightHeight, paint);
		paint.setColor(colorA);
		if (l2)
			paint.setColor(colorB);
		canvas.drawRect(l += lightSpace, t, l += lightWidth, t + lightHeight,
				paint);
		paint.setColor(colorA);
		if (l3)
			paint.setColor(colorB);
		canvas.drawRect(l += lightSpace, t, l += lightWidth, t + lightHeight,
				paint);

		// draw bottom lights
		l = left - 4;
		t = bodyBottom + 16;

		paint.setColor(colorA);
		if (l4)
			paint.setColor(colorB);
		canvas.drawRect(l, t, l += lightWidth, t + lightHeight, paint);
		paint.setColor(colorA);
		if (l5)
			paint.setColor(colorB);
		canvas.drawRect(l += lightSpace, t, l += lightWidth, t + lightHeight,
				paint);
		paint.setColor(colorA);
		if (l6)
			paint.setColor(colorB);
		canvas.drawRect(l += lightSpace, t, l += lightWidth, t + lightHeight,
				paint);
	}

	public void drive() {
		step = !step;
	}

	public void clearLights() {
		l1 = l2 = l3 = l4 = l5 = l6 = false;
	}
}
